package com.sx.admin.controller.plat;

import com.sx.admin.controller.BaseController;
import com.sx.core.mybatis.SessionTemplate;
import com.sx.core.utils.PropertyUtils;
import com.sx.core.utils.QrUtils;
import com.sx.core.utils.UUIDUtils;
import com.sx.core.vo.JsonReaderResponse;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.Map;

/**
 * Created by lihao on 16/12/21.
 */
@RestController
@RequestMapping("pl/device")
public class DeviceController extends BaseController {
    @Autowired
    SessionTemplate sessionTemplate;
    @Autowired
    JdbcTemplate jdbcTemplate;
    private String expireIn= PropertyUtils.getProperty("api.token.expire");
    private String secret=PropertyUtils.getProperty("api.token.secret");
    private String fileLocation= PropertyUtils.getProperty("file.location");

    @RequestMapping(value = "list")
    public JsonReaderResponse list() {
        return new JsonReaderResponse(jdbcTemplate.queryForList("SELECT d.*,l.name as labname FROM t_device d JOIN t_laboratory l ON d.labid=l.id ORDER BY d.createdate DESC "));

    }
    @RequestMapping(value = "save")
    public JsonReaderResponse uploadPartScore(@RequestParam Map<String,Object> params) {

        String id= ObjectUtils.toString(params.get("id"));
        String no= ObjectUtils.toString(params.get("no"));
        if(StringUtils.isEmpty(id)){
//            long count=jdbcTemplate.queryForObject("SELECT count(*) FROM t_device WHERE no=?",Long.class,no);
//            if(count>0){
//                return new JsonReaderResponse("设备编号已存在",101);
//            }
            params.put("id", UUIDUtils.generateUuid32());
            params.put("createdate",new Date());
            namedParameterJdbcTemplate.update("insert into t_device (id,name,labid,createdate) values (:id,:name,:labid,:createdate);",params);
        }else{
//            long count=jdbcTemplate.queryForObject("SELECT count(*) FROM t_device WHERE no=? AND id!=?",Long.class,no,id);
//            if(count>0){
//                return new JsonReaderResponse("设备编号已存在",101);
//            }
            namedParameterJdbcTemplate.update("update t_device set  name=:name,labid=:labid where id=:id",params);
        }
        return new JsonReaderResponse();

    }
    @RequestMapping(value = "delete")
    public JsonReaderResponse uploadAlltScore( String id) {
        jdbcTemplate.update("DELETE FROM t_device WHERE id=?",id);
        return new JsonReaderResponse();

    }

    @RequestMapping(value = "devices")
    public ModelAndView labs(Model model) {
        return new ModelAndView("plat/device/devices");
    }
    @RequestMapping(value = "update")
    public ModelAndView update(String id,Model model) {
        if(StringUtils.isNotBlank(id)){
            model.addAttribute("command",jdbcTemplate.queryForMap("SELECT * FROM t_device WHERE id=?",id));
        }
        model.addAttribute("labs",jdbcTemplate.queryForList("SELECT * FROM t_laboratory ORDER BY createtime DESC"));
        return new ModelAndView("plat/device/update");
    }
     @RequestMapping(value = "download_qr")
    public ModelAndView download_qr(String id, HttpServletResponse response) {
        Map<String,Object> map=jdbcTemplate.queryForMap("SELECT * FROM t_device WHERE id=?",id);
        String fileName=ObjectUtils.toString(map.get("name"));
         fileName=fileName+".png";

         try {
             response.setContentType("image/png");
             response.setHeader("content-disposition", "attachment;fileName="+ URLEncoder.encode(fileName, "UTF-8"));
             QrUtils.downloadQr(id,260,260,response.getOutputStream());
         } catch (IOException e) {
             logger.error("download_qr",e);
         }

         return null;
    }










}
